<template>
  <div class="container">
    <!-- 固定导航 -->
    <van-nav-bar fixed left-arrow @click-left="$router.back()" title="小智同学"></van-nav-bar>

    <!-- 聊天主体区域 -->
    <div class="chat-list">
      <!-- 左侧是机器人小智 -->
      <!-- 右侧是当前用户 -->
      <div class="chat-item" v-for="(item,index) in list" :class="item.type === 'xz'?'left':'right'" :key="index">
        <template v-if="item.type==='xz'">
          <van-image fit="cover" round src="https://img.yzcdn.cn/vant/cat.jpeg" />
          <div class="chat-pao">{{item.msg}}</div>
        </template>
        <template v-else>
          <div class="chat-pao">{{item.msg}}</div>
          <van-image fit="cover" round :src="$store.state.userInfo.photo" />
        </template>
      </div>
    </div>
    <!-- 对话区域 -->
    <div class="reply-container van-hairline--top">
      <van-field v-model.trim="word" placeholder="说点什么...">
        <span  @click="send" slot="button" style="font-size:12px;color:#999">提交</span>
      </van-field>
    </div>
  </div>
</template>

<script>
import { io } from 'socket.io-client'
export default {
  name: 'UserChat',
  data () {
    return {
      word: '', // 输入框需要发送给后端的的消息
      list: []
      // 存放双方发送的消息  俩种模板下 我需要一个数据标识 标识一下当前的消息类型
      // 消息类型是指当前的消息是前端发给后端的还是后端发给前端的
    }
  },
  methods: {
    send () {
      console.log(this.word)
      // 客户端向服务端发送消息
      this.socket.emit('chat message', this.word)
      this.list.push({
        msg: this.word,
        type: 'my'
      })
      this.word = ''
    }
  },
  created () {
    // 1. 当我们在多个组件配置项都需要用到某个对象的时候可以把它挂载到当前组件的实例上
    // 2. 因为响应式处理在created调用之前  所有在created中再往实例上挂载属性都不会变为响应式

    // 客户端发起连接请求 默认以当前地址localhost:8080为准
    this.socket = io()
    // 2. 建立连接
    this.socket.on('connect', () => {
      console.log('建立连接成功了')
    })
    // 3.监听消息返回
    this.socket.on('chat message', msg => {
      console.log('服务回来的消息', msg)
      // 将后端返回来的消息push到list
      this.list.push({
        msg,
        type: 'xz'
      })
    })
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  width: 100%;
  position: absolute;
  left: 0;
  top: 0;
  box-sizing: border-box;
  background:#fafafa;
  padding: 46px 0 50px 0;
  .chat-list {
    height: 100%;
    overflow-y: scroll;
    .chat-item{
      padding: 10px;
      .van-image{
        vertical-align: top;
        width: 40px;
        height: 40px;
      }
      .chat-pao{
        vertical-align: top;
        display: inline-block;
        min-width: 40px;
        max-width: 70%;
        min-height: 40px;
        line-height: 38px;
        border: 0.5px solid #c2d9ea;
        border-radius: 4px;
        position: relative;
        padding: 0 10px;
        background-color: #e0effb;
        word-break: break-all;
        font-size: 14px;
        color: #333;
        &::before{
          content: "";
          width: 10px;
          height: 10px;
          position: absolute;
          top: 12px;
          border-top:0.5px solid #c2d9ea;
          border-right:0.5px solid #c2d9ea;
          background: #e0effb;
        }
      }
    }
  }
}
.chat-item.right{
  text-align: right;
  .chat-pao{
    margin-left: 0;
    margin-right: 15px;
    &::before{
      right: -6px;
      transform: rotate(45deg);
    }
  }
}
.chat-item.left{
  text-align: left;
  .chat-pao{
    margin-left: 15px;
    margin-right: 0;
    &::before{
      left: -5px;
      transform: rotate(-135deg);
    }
  }
}
.reply-container {
  position: fixed;
  left: 0;
  bottom: 0;
  height: 44px;
  width: 100%;
  background: #f5f5f5;
  z-index: 9999;
}
</style>
